package 数组的相对排序;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
//1.自定义排序
public class Solution1 {
    public static int[] relativeSortArray(int[] arr1, int[] arr2) {
        ArrayList<Object> list = new ArrayList<>();
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int num : arr1) {
            list.add(num);
        }
        for (int i = 0; i < arr2.length; i++) {
            map.put(arr2[i],i);
        }

        Collections.sort(list, (x, y) -> {
            if (map.containsKey(x) || map.containsKey(y)) {
                return map.getOrDefault(x, 1001) - map.getOrDefault(y, 1001);
            }
            return (Integer) x - (Integer) y;
        });

        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = (int) list.get(i);
        }
        return  arr1;
    }
}